package com.example.compositepattern.entity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ash
 * @description
 * @create 2023/2/20  22:17:10
 * @className Composite
 * @verson 1.0.0
 */
public class Composite extends Component{

    private List<Component> componentList = new ArrayList<>();

    public Composite(String name) {
        super(name);
    }

    @Override
    public void add(Component component) {
        componentList.add(component);
    }

    @Override
    public void remove(Component component) {
        componentList.remove(component);
    }

    @Override
    public void display(int depth) {
        StringBuilder s = new StringBuilder("-");
        for (int i = 0; i < depth; i++) {
            s.append("-");
        }
        System.out.println(s + name);
        componentList.forEach(item ->{
            item.display(depth + 2);
        });
    }
}
